package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.Menu;
import com.itheima.VO.setmeal_ids;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MenuService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MenuService menuService;

    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/getMenu")
    public Result getMenu(){
        try {
            List<Menu> list=menuService.findMenu();
            return new Result(true,"菜单查询成功",list);
        } catch (Exception e) {
            return new Result(true,"菜单查询失败");
        }
    }

    @RequestMapping("/findpage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageResult pageResult=  menuService.findPage(currentPage,pageSize);
        return pageResult;
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //前段传过来的 参数名称 是否为imgFile 不是的话强制为imgFile

        //此时的imgFile实际上是 该文件的对象,该文件信息在此对象中
        try {
            //得到文件原来的名称
            String originalFilename = imgFile.getOriginalFilename();
            //不用原本的文件名,用后面随机生成的字符串作为文件名,而需要自行补充一个后缀
            //从而我们应该自己得到原文件的后缀名
            //为了找到最后的后缀名(带.的) 拿到最后一个点的位置 进行截取
            int index = originalFilename.lastIndexOf(".");
            //截取到文件的后缀名
            String suffix = originalFilename.substring(index);
            //用UUID得到个随机的唯一的字符串 把它加上后缀  作为保存的图片名
            String fileName = UUID.randomUUID().toString() + suffix;
            //用七牛云的工具类进行上传 参数为 文件的字节数组  和 文件名
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //保存到七牛云以后 将文件名保存到 对应的redis的set集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES_MENU,fileName);
            //返回结果
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //出错的话 就返回没有上传成功
            return  new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/add")
    public  Result addSetmeal(@RequestBody Menu menu){
        //{"disable":"0","name":"1234","parentMenuId":"1234","priority":"123","linkUrl":"1234","img":"4b22faaf-033a-4b35-8ce9-78a668703c30.jpg"}
      //  System.out.println(menu);

        try {
            menuService.addmenu(menu);
            //保存好了说明图片的名字 放到数据库中  ,把保存到数据库中的img名字放到对应的redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES_MENU,menu.getIcon());
            return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            return  new Result(false,e.getMessage());
        }
    }

    @RequestMapping("/findMenuByID")
    public Result findMenuByID(Integer id){
       Menu   menu= menuService.findMenuByid(id);
       if (menu !=null){
           return new Result(true,"成功",menu);
       }else {
           return  new Result(false,"出现错误");
       }
    }

    @RequestMapping("/editMenu")
    public  Result editMenu(@RequestBody Menu menu){
        try {
            menuService.editMenu(menu);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
